/*============================================================================*/
/*                        Tortoise Team                              		  */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %MAL_UART.c%
* Instance:         RPL_1
* %version:         1 %
* %created_by:      Oscar Rodea Aragon %
* %date_created:    Sunday Aug 26 00:11:00 2012 %
*=============================================================================*/
/* DESCRIPTION : Source file for uC Abstraction layer for UART module         */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the functions for uC Abstraction Layer for    */
/*	UAR module																  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 26/08/2012  |                               | Oscar Rodea	  */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "MAL_UART.h"
//#include "MAL_IOAPI.h"
#include "stdtypedef.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */

/* WORD constants */

/* LONG and STRUCTURE constants */


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

/* WORD RAM variables */

/* LONG and STRUCTURE RAM variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* Private functions prototypes */
/* ---------------------------- */


/* Exported functions prototypes */
/* ----------------------------- */
void 	vfnInitLINFlex(T_UBYTE ub_baudRate);
void 	vfnTx_byte(T_UBYTE ub_value);
void 	vfnTx_word(T_UWORD uw_value);
void 	vfnTx_StringByte(T_UBYTE ub_value[]);
void 	vfnTx_StringWord(T_UWORD uw_value[]);
T_UBYTE ubfnRx_byte();

/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */

/* Exported functions */
/* ------------------ */

/**************************************************************
 *  Name                 : 	vfnInitLINFlex
 *  Description          :	Init a UART port
 *  Parameters           :  [T_UBYTE ub_baudRate]
 *  Return               :	void
 *  Critical/explanation :  The acceptable values are 1 - 2400BR, 2 - 9600BR and 3 - 115200BR 
 **************************************************************/
/*! Initialize the UART port
   \param ub_baudRate Select the baud rate, 1 - 2400, 2 - 9600 and 3 - 115200
   \sa Version 
  */
void vfnInitLINFlex(T_UBYTE ub_baudRate)
{
  LINFLEX_0.LINCR1.B.INIT = 1;    			/* Put LINFlex hardware in init mode */
  LINFLEX_0.LINCR1.R= 0b10001;    			/* Configure module as LIN master*/
  LINFLEX_0.UARTCR.R= 0b110000000110011;   	/* Configure module as UART 1 byte no parity*/
  
  /*Baud rate*/
  if(ub_baudRate == 1)		//2400BR
  {
  	LINFLEX_0.LINIBRR.B.DIV_M = 1666; 		/* Mantissa baud rate divider 1666 component 2400*/
  	LINFLEX_0.LINFBRR.B.DIV_F = 11;  		/* Fraction baud rate divider 11 component 2400*/
  }
  else if(ub_baudRate == 3)	//115200BR 
  {
  	LINFLEX_0.LINIBRR.B.DIV_M = 34; 		/* Mantissa baud rate divider 34 component 115200*/
  	LINFLEX_0.LINFBRR.B.DIV_F = 12;  		/* Fraction baud rate divider 12 component 115200*/
  }
  else		//Default 9600BR
  {
  	LINFLEX_0.LINIBRR.B.DIV_M = 416; 		/* Mantissa baud rate divider 416 component 9600*/
  	LINFLEX_0.LINFBRR.B.DIV_F = 11;  		/* Fraction baud rate divider 11 component 9600*/
  }
 
  // IRQ#79		Offset:0x093C		Size(bytes):4 		Interrupt:LINFlex_RXI		Module:LINFlex_0
  LINFLEX_0.LINIER.B.DRIE = 1;				/* Interrupt enable */
  
  LINFLEX_0.UARTCR.R= 0b110000000110011;  	/* Configure module as UART 1 byte no parity*/
  LINFLEX_0.LINCR1.B.INIT = 0;       		/* Put LINFlex hardware in normal mode */
  SIU.PCR[18].R = 0x0400;         			/* MPC56xxB: Configure port B2 as LIN0TX */
  SIU.PCR[19].R = 0x0103;         			/* MPC56xxB: Configure port B3 as LIN0RX */
}

/**************************************************************
 *  Name                 : vfnTx_byte
 *  Description          : Transmit a byte
 *  Parameters           : [T_UBYTE ub_value]
 *  Return               : void
 *  Critical/explanation : No
 **************************************************************/
/*! Transmit a byte
   \param ub_value Value to transmit
   \sa Version 
  */
void vfnTx_byte(T_UBYTE ub_value)
{
	LINFLEX_0.UARTCR.B.TDFL=0;
	LINFLEX_0.BDRL.B.DATA0 = ub_value;
  	
  	while(LINFLEX_0.UARTSR.B.DTF == 0);
  	
  	LINFLEX_0.UARTSR.B.DTF = 1;
}

/**************************************************************
 *  Name                 : vfnTx_word
 *  Description          : Transmit a word
 *  Parameters           : [T_UWORD uw_value]
 *  Return               : void
 *  Critical/explanation : No
 **************************************************************/
/*! Transmit a word
   \param uw_value Value to transmit
   \sa Version 
  */
void vfnTx_word(T_UWORD uw_value)
{
	T_UBYTE MSB, LSB;
	MSB = (T_UBYTE)(uw_value & 0xFF00) >> 8;
	LSB = (T_UBYTE)(uw_value & 0x00FF);
	vfnTx_byte(MSB);
	vfnTx_byte(LSB);
}

/**************************************************************
 *  Name                 : vfnTx_StringByte
 *  Description          : Transmit a string of bytes
 *  Parameters           : [T_UBYTE ub_value[]]
 *  Return               : void
 *  Critical/explanation : If you are going to put a array,
 						   it has to finish with 0x00
 **************************************************************/
/*! Transmit string of bytes
   \param ub_value[] String to transmit
   \sa Version 
  */
void vfnTx_StringByte(T_UBYTE ub_value[])
{
	T_UBYTE count=0;
	do
	{
		vfnTx_byte(ub_value[count]);
		count++;
	}while(ub_value[count] != 0x00);
}

/**************************************************************
 *  Name                 : vfnTx_StringWord
 *  Description          : Transmit a string of words
 *  Parameters           : [T_UWORD uw_value[]]
 *  Return               : void
 *  Critical/explanation : If you are going to put a array,
 						   it has to finish with 0x00
 **************************************************************/
/*! Transmit string of words
   \param uw_value[] String to transmit
   \sa Version 
  */
void vfnTx_StringWord(T_UWORD uw_value[])
{
	T_UBYTE count=0;
	do
	{
		vfnTx_word(uw_value[count]);
		count++;
	}while(uw_value[count] != 0x00);
}

/**************************************************************
 *  Name                 : vfnRx_byte
 *  Description          : Receive a byte
 *  Parameters           : void
 *  Return               : T_UBYTE
 *  Critical/explanation : No
 **************************************************************/
/*! Reveive a byte
   \param T_UBYTE The byte that arrive.
   \sa Version 
  */
T_UBYTE ubfnRx_byte()
{
	T_UBYTE receive = 0;
  	if(LINFLEX_0.UARTSR.B.DRF==1)
  	{
    	receive = (T_UBYTE) LINFLEX_0.BDRM.B.DATA4;
  	}
  	else
  	{
    	receive = 0;
  	}
  	LINFLEX_0.UARTSR.B.DRF = 1;
  	return receive;
}


